import omsRequest from "/utils/omsRequest";
const { showToast } = require("/utils/common");
import { validListItem } from "/utils/commonMethod";
import { createDeposit } from "/api/distributeAchievement";
const { moment } = require("/utils/core");
Component({
  mixins: [],
  data: {
    wholeCountry: {},
    depositList: [],
    undistributedFlag: false // 销售主管 组长 待分配收款金额 不能超过目标值
  },
  props: {
    title: [],
    list: [],
    vo: {},
    onChangeList: (list) => console.log(list),
    onChangeVo: (vo) => console.log(vo)
  },
  didMount() {
    // 列表回显-total初始化
    this.props.list.forEach((item) => {
      if (item.nodeUsers.length) {
        item.total = item.nodeUsers[0].saleTargetDeposit && item.nodeUsers[0].saleTargetDeposit.total / 10000
      } else {
        item.total = null;
      }
    })
    this.props.onChangeList(this.props.list)
    this.setData({
      depositList: this.props.list,
      wholeCountry: this.props.vo
    })
  },
  didUpdate() {
  },
  didUnmount() { },
  methods: {
    onInput(e) {
      this.data.depositList.find((item) => item.id === e.currentTarget.dataset.item.id).total = e.detail.value < 0 ? 0 : e.detail.value
      if (this.props.title.includes('名称')) { // 销售主管和组长 计算剩余待分配业绩
        this.props.onChangeList(this.data.depositList)
      }
      this.setData({
        depositList: this.data.depositList
      })
    },
    onSubmit() {
      let total = 0;
      let list = this.data.depositList;
      let paramsVo = this.props.vo;
      paramsVo.saleTargetList = []
      // 校验 销售主管 组长 待分配收款金额 不能超过目标值
      if (this.props.title.includes('名称')) {
        list.forEach(item => {
          total += +item.total
        });
        if (total !== this.props.vo.total / 10000) {
          showToast({
            msg: '分配业绩总和和收款目标不一致。请重新分配!'
          })
          return
        }
      }
      if (validListItem({ list, name: "收款额", role: this.props.title.includes('名称') ? '人员' : '城市' })) {
        // 参数处理
        list.forEach((item) => {
          item.nodeUsers.forEach((value) => {
            paramsVo.saleTargetList.push({
              dingUserId: value.dingUserId,
              total: item.total * 10000 // 单位是万
            })
          })
        })
        // 处理日期格式 -后端解析失败
        paramsVo.createTime = this.props.vo.createTime && moment(this.props.vo.createTime, "YYYY-MM-DD HH:mm:ss");
        paramsVo.updateTime = this.props.vo.updateTime && moment(this.props.vo.updateTime, "YYYY-MM-DD HH:mm:ss");
        delete paramsVo.rotateValue
        dd.confirm({
          title: '提示!',
          content: '请确认目标收款额分配无误!',
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          success: (result) => {
            result.confirm && this.createDeposit(paramsVo)
          },
        });
      }
    },
    onReset(e) {
      this.data.depositList.forEach((item) => {
        if (item.total) {
          item.total = null
        }
      })
      this.setData({
        depositList: this.data.depositList
      })
    },
    createDeposit(params) {
      dd.showLoading()
      omsRequest({
        url: createDeposit,
        method: "post",
        data: params,
        headers: {
          "content-type": "application/json"
        }
      }).then((res) => {
        if (res.success) {
          dd.alert({
            content: this.props.title.includes('名称') ? '目标收款额分配成功' : '收款金额分配成功',
            buttonText: '确定',
            success: () => {
              this.setData({
                'wholeCountry.distributableFlag': true // 配置成功
              })
              this.props.onChangeVo(this.data.wholeCountry)
              dd.hideLoading()
            },
          });
        }
      })
    }
  },
});
